//Function checkAll
function checkAll(obj)
{
	$list = document.getElementsByName('cb[]');
	//Checked
	if (obj.checked == true) {
		for (var i = $list.length - 1; i >= 0; i--) {
			$list[i].checked = true;
		};
	}
	//Unchecked
	else {
		for (var i = $list.length - 1; i >= 0; i--) {
			$list[i].checked = false;
		};
	}
}
function isChecked(value)
{
	if (value == false)
		document.getElementById('checkall').checked = value;
}
function getConditions()
{
	var cat_id = document.getElementById('categoryid');
	var state = document.getElementById('state');
	var author = document.getElementById('author');
	var conditions = '';
	if (cat_id.value !== '0') {
		conditions = conditions + 'p.cat_id=' + cat_id.value + ' AND ';
	}
	if (state.value !== '0') {
		conditions = conditions + 'p.state=' + state.value + ' AND ';	
	}
	else {
		conditions = conditions + '(p.state=1 OR p.state=2) AND ';		
	}
	if (author.value !== 'all') {
		conditions = conditions + 'p.created_by=\'' + author.value + '\' AND ';
	}
	if (conditions !== '') {
		var len = conditions.length;
		conditions = conditions.substring(0,len - 5);
		conditions = 'WHERE ' + conditions;
	}
	return conditions;
}
function changeConditions()
{
	//Remove the message
	var error = document.getElementById('error-message');
	error.innerHTML = '';
	error.setAttribute('class','');
	switchTrashAndDelete(); //Trash <-> Delete
	sendNewConditionsRequest(1);
}
function sendNewConditionsRequest(page)
{
	//Get the conditions
	var conditions = getConditions();
	//Ajax
	if (window.XMLHttpRequest) {
		xmlhttp = new window.XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
			var response = xmlhttp.responseText;
			var viewposts = document.getElementById('viewposts');
			viewposts.innerHTML = response;
		}
	}
	//Create paramaters
	parameters = 'conditions=' + conditions + '&page='+ page + '&action=reload';

	xmlhttp.open('POST','scripts/get-posts.php',true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(parameters);
	return false; //Don't submit form
}
function update_post_state(obj)
{	
	var parent = obj.parentNode.parentNode;
	var id = parent.firstChild.firstChild.value;
	var objChild = obj.firstChild;
	var state = 1;
	if (objChild.innerHTML == 'publish')
		state = 2;
	var update_conditions = 'id=' + id;
	var value = 'state=' + state;
	var conditions = getConditions();
	//Send update request
	sendUpdateRequest(conditions,update_conditions,value,1);
}
function getPostsIds()
{
	var update_conditions = '';
	var list = document.getElementsByName('cb[]');
	var len = list.length;
	for (var i = 0; i < len; i++) {
		if (list[i].checked == true) {
			update_conditions = update_conditions + 'id=' + list[i].value + ' OR ';
		}
	}
	if (update_conditions !== '') {
		len = update_conditions.length;
		update_conditions = update_conditions.substring(0,len - 4);
	}
	return update_conditions;
}
function countPostsIds()
{
	var count = 0;
	var list = document.getElementsByName('cb[]');
	var len = list.length;
	for (var i = 0; i < len; i++) {
		if (list[i].checked == true) {
			count = count + 1;
		}
	}
	return count;
}
function update_posts_state(obj)
{
	//Get posts ids
	var update_conditions = getPostsIds();
	if (update_conditions == '')
		return false;
	var state;
	if (obj.id == 'publish')
		state = 1;
	else if (obj.id == 'unpublish')
		state = 2;
	else
		state = 3;
	var value = 'state=' + state;
	var conditions = getConditions();
	var count = countPostsIds();
	sendUpdateRequest(conditions,update_conditions,value,count);
}
function sendUpdateRequest(conditions,update_conditions,value,count)
{
	//Ajax create
	if (window.XMLHttpRequest) {
		xmlhttp = new window.XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
			var response = xmlhttp.responseText;
			var viewposts = document.getElementById('viewposts');
			viewposts.innerHTML = response;
			//Show the message
			var error = document.getElementById('error-message');
			error.setAttribute('class','success');
			if (count == 1)
				error.innerHTML = '1 post updated successful';
			else
				error.innerHTML = count +' posts updated successful';
		}
	}
	//Create paramaters
	parameters = 'conditions=' + conditions + '&update_conditions=' + update_conditions + '&value=' + value +'&action=update';
	//Send request
	xmlhttp.open('POST','scripts/get-posts.php',true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(parameters);
	return false; //Don't submit form
}
function changePage(obj)
{
	//Remove the message
	var error = document.getElementById('error-message');
	error.innerHTML = '';
	error.setAttribute('class','');
	//Load a new page
	var page = obj.text;
	sendNewConditionsRequest(page);
}
//Edit post
function editpost()
{
	var i;
	$list = document.getElementsByName('cb[]');
	var len = $list.length;
	for (i = 0; i < len; i++) {
		if ($list[i].checked == true) {
			break;
		}
	};
	if (i == len) {
		alert('You need to choose a post!');
		return false;
	}
	else {
		window.location = 'create-posts.php?id=' + $list[i].value;
	}
}
//When trash posts loaded - Change Trash button to Delete button
function switchTrashAndDelete()
{
	var state = document.getElementById('state');
	if (state.value == '3') {
		var trash = document.getElementById('trash');
		if (trash != null) {
			var parent = trash.parentNode;
			parent.innerHTML = '<a href="#" class="toolbar" id="delete" onclick="deletePosts();"><span class="delete"></span>Delete</a>';
		}
	}
	else {
		var del = document.getElementById('delete');
		if (del != null) {
			var parent = del.parentNode;
			parent.innerHTML = '<a href="#" class="toolbar" id="trash" onclick="update_posts_state(this);"><span class="trash"></span>Trash</a>';		
		}
		
	}
}
//Call delete query
function deletePosts()
{
	var count = 0;
	var list = document.getElementsByName('cb[]');
	var len = list.length;
	var update_conditions = '';
	for (var i = 0; i < len; i++) {
		if (list[i].checked == true) {
			update_conditions = update_conditions + 'id=' + list[i].value + ' OR ';
			count = count + 1;
		}
	}
	if (update_conditions == '') {
		alert('Please choose one or more posts!');
		return false;
	}
	len = update_conditions.length;
	update_conditions = update_conditions.substring(0,len - 4);
	update_conditions = '(' + update_conditions + ')' + ' AND state=3';
	//Get conditions
	var conditions = getConditions();
	//Ajax create
	if (window.XMLHttpRequest)
		xmlhttp = new window.XMLHttpRequest();
	else
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
			var response = xmlhttp.responseText;
			var viewposts = document.getElementById('viewposts');
			viewposts.innerHTML = response;
			//Show the message
			var error = document.getElementById('error-message');
			error.setAttribute('class','success');
			if (count == 1)
				error.innerHTML = 'Delete 1 post successful';
			else
				error.innerHTML = 'Delete ' + count + ' posts successful';
		}
	}
	//Create paramaters
	paramaters = 'conditions=' + conditions + '&update_conditions=' + update_conditions + '&action=delete';
	//Send ajax request
	xmlhttp.open('POST','scripts/get-posts.php',true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(paramaters);
	return false;
}
$(function(){
	
});